{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9b26f625",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "34acc88a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ab'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = re.compile('(a)b')\n",
    "m = p.match('ab')\n",
    "m.group()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9ea4c5d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ab'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.group(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7f842467",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.group(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "53891a41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a',)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.groups()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b7644d0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "p = re.compile('(a(b)c)d')\n",
    "m = p.match('abcd')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b95fed26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abcd'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.group()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8831816e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('abc', 'b')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.groups()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8ce7e39",
   "metadata": {},
   "source": [
    "模式中的后向引用允许你指定还必须在字符串中的当前位置找到先前捕获组的内容。 例如，如果可以在当前位置找到组 1 的确切内容，则 `\\1` 将成功，否则将失败。 请记住，Python 的字符串文字也使用反斜杠后跟数字以允许在字符串中包含任意字符，因此正则中引入反向引用时务必使用原始字符串。\n",
    "\n",
    "例如，以下正则检测字符串中重复的单词。:\n",
    "```\n",
    "p = re.compile(r'\\b(\\w+)\\s+\\1\\b')\n",
    "p.search('Paris in the the spring').group()\n",
    "'the the'\n",
    "```\n",
    "像这样的后向引用通常不仅仅用于搜索字符串 —— 很少有文本格式以这种方式重复数据 —— 但是你很快就会发现它们在执行字符串替换时 非常 有用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c8121a9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'the the'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = re.compile(r'\\b(\\w+)\\s+\\1\\b')\n",
    "p.search('Paris in the the spring').group()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "194b955f",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = re.match(\"([abc])+\", \"abc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "020a778a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abc'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.group()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "297b4231",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('c',)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.groups()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "992fa6f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = re.search(\"([abc])+\", \"abc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6f80dc5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('c',)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.groups()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "49658b87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['c']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.findall(\"([abc])+\", \"abc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "206a1ad2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 3), match='abc'>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = re.match(\"[abc]+\", \"abc\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "15920459",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.groups()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1ae04b39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('abc',)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = re.match(\"([abc]+)\", \"abc\")\n",
    "m.groups()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adebd693",
   "metadata": {},
   "source": [
    "A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data\n",
    "重复捕获组只捕获最后一次迭代。\n",
    "在重复组周围设置一个捕获组来捕获所有迭代，或者如果您对数据不感兴趣，则改用非捕获组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "ef26713f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('c',)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = re.match(\"([abc])+\", \"abc\")\n",
    "m.groups()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "0d74bea5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('c',)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = re.match(\"([abc])+\", \"abcd\")\n",
    "m.groups()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a1f015a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abc'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.group()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6882dba2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('c',)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = re.match(\"([abc])+\", \"abc\")\n",
    "m.groups()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5322ffa5",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = re.match(\"([abc])+\", \"abc\")\n",
    "m.groups()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
